package com.honey.service.method;

import java.util.Set;

import com.honey.compilation.java.JavaKeyWord;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.Parameter;
import com.honey.core.generator.PresentationLayerCompilation;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.generator.java.AbstractClassName;
import com.honey.service.SampleServicePlugin;

public class ServiceMethod extends AbstractClassName{
	
	private SampleServicePlugin sampleServicePlugin;
	
	private PresentationLayerCompilation presentationLayer;
	
	
	public ServiceMethod(
			SampleServicePlugin sampleServicePlugin,
			PresentationLayerCompilation presentationLayer
			
		){
		super(sampleServicePlugin);
		this.sampleServicePlugin =  sampleServicePlugin;
		this.presentationLayer =  presentationLayer;
	}
	
	public Method[] createMethod(){
		Method[] answer = null;
		FullyQualifiedJavaType type = presentationLayer.layerType();
		String name = super.getVariableName(type.getShortName());
		Method[] layerPublicMethod = presentationLayer.layerPublicMethod();
		answer = new Method[layerPublicMethod.length];
		Method method ,layerPublic = null;
		for(int i =0;i<layerPublicMethod.length ;i++  ){
			layerPublic = layerPublicMethod[i]; 
			method = new Method(layerPublic.getName(),layerPublic.getReturnType());
			Set<Parameter> parameters  =  layerPublic.getParameters();
			StringBuilder sb = new StringBuilder();
			sb.append(JavaKeyWord.RETURN)
			.append(name)
			.append(".")
			.append(layerPublic.getName())
			.append(JavaKeyWord.OPERATION_BRACKET_LEFT);
			boolean bool = false;
			if( parameters.size() > 0 ){
				for( Parameter parameter : parameters){
					method.addParameter(parameter);
					if(bool)sb.append(", ");else bool=true;
					sb.append(parameter.getName());
				}
			}
			sb.append(JavaKeyWord.OPERATION_BRACKET_RIGHT);
			Set<FullyQualifiedJavaType> exceptions = layerPublic.getExceptions();
			if( exceptions.size() > 0 ){
				for( FullyQualifiedJavaType exception : exceptions){
					method.addException(exception);
				}
			}
			method.addBodyLine(sb.toString());
			answer[i] = method;
		}
		
		
		return answer ;
	}
}
